﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace PeekWebApp
{
    public class AuthenticationModule:IHttpModule

    {
        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
            
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;
            //if (context.Request.Url.AbsoluteUri.ToLower().IndexOf("ckfinder") > -1
            //    || context.Request.Url.AbsoluteUri.ToLower().IndexOf("ckeditor") > -1
            //    || context.Request.Url.AbsoluteUri.ToLower().IndexOf("management") > -1)
            #region session版本
            //HttpSessionState session= context.Session;
            //if (session != null)
            //{
            //    if (context.Request.Url.AbsoluteUri.ToLower().IndexOf("management") > -1)
            //    {

            //        if (context.Request.Cookies["User"].Value == null)
            //        {

            //            context.Response.Redirect("~/admin.aspx");
            //        }
            //    }
            //}
            //else
            //{
            //    if (context.Request.Url.AbsoluteUri.ToLower().IndexOf("management") > -1)
            //    {
            //        context.Response.Redirect("~/admin.aspx");
            //    }
            //}          
            HttpCookie cookie = context.Request.Cookies["UserAdmin"];
            if (cookie == null)
            {
                if (context.Request.Url.AbsoluteUri.ToLower().IndexOf("management") > -1)
                {
                    context.Response.Redirect("~/admin.aspx");
                }
            }           
            #endregion
        }
    }
}